﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Hotel.UI.Common
{
    public class MultiParamterConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //必须新new一个，否则拿不到数据，因为values在返回之后，就会被清空了
            return values.Clone();
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    } 
    public class GuestCount2PathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            if (System.Convert.ToInt32(value) > 5)
                value = "5";
            if (System.Convert.ToInt32(value) < 1)
                return "";
            return "path_men" + value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class Base64ImageSourceConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var base64 = value.ToString();
                string imagebase64 = base64.Substring(base64.IndexOf(",") + 1);
                byte[] streamBase = System.Convert.FromBase64String(imagebase64);
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(streamBase);
                bi.EndInit();
                return bi;
                //string source = value.ToString();
                //var ims = new InMemoryRandomAccessStream();
                //var bytes = Convert.FromBase64String(source);
                //var dataWriter = new DataWriter(ims);
                //dataWriter.WriteBytes(bytes);
                //dataWriter.StoreAsync();
                //ims.Seek(0);
                //var img = new BitmapImage();
                //img.SetSource(ims);
                //return img;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}